import { fetchCaseTypeListAPI } from '@/apis/fetchCaseTypeList'
import { fetchExtentListAPI } from '@/apis/fetchExtentList'
import { fetchHouseTypeListAPI } from '@/apis/fetchHouseTypeList'
import { fetchNodeListAPI } from '@/apis/fetchNodeList'
import { fetchProjectTypeListAPI } from '@/apis/fetchProjectTypeList'

export const useProjectConfigStore = defineStore('projectConfig', {
  state: () => {
    return {
      // 面积列表
      extentList: [] as Extent[],
      // 户型列表
      houseTypeList: [] as HouseType[],
      // 装修类型列表
      projectTypeList: [] as ProjectType[],
      // 案例类型列表
      caseTypeList: [] as CaseType[],
      // 每种项目类型对应的节点列表
      nodeLists: {} as Record<string, ProjectNode[]>,

      // 项目状态
      projectStatusStr: {
        1: '审核中',
        2: '施工中',
        3: '售后中',
      } as Record<string, string>,
      projectStatusCode: {
        审核中: '1',
        施工中: '2',
        售后中: '3',
      } as Record<string, string>,
      // 项目审核状态
      projectVerifyStatusStr: {
        1: '待审核',
        2: '审核通过',
        3: '审核驳回',
      } as Record<string, string>,
      projectVerifyStatusCode: {
        待审核: '1',
        审核通过: '2',
        审核驳回: '3',
      } as Record<string, string>,
      // 售后状态
      afterSalesStatusStr: {
        1: '待处理',
        2: '处理中',
        3: '已完成',
      } as Record<string, string>,
      afterSalesStatusCode: {
        待处理: '1',
        处理中: '2',
        已完成: '3',
      } as Record<string, string>,
    }
  },
  actions: {
    async fetchExtentList() {
      try {
        const res = await fetchExtentListAPI()
        if (res.data?.code === 1)
          this.extentList = res.data.data
        else
          uni.showToast({ title: `${res.data?.msg}`, icon: 'none' })
      }
      catch (error) {
        uni.showToast({ title: `请求错误，错误信息：${error}` })
      }
    },
    async fetchHouseTypeList() {
      try {
        const res = await fetchHouseTypeListAPI()
        if (res.data?.code === 1)
          this.houseTypeList = res.data.data
        else
          uni.showToast({ title: `${res.data?.msg}`, icon: 'none' })
      }
      catch (error) {
        uni.showToast({ title: `请求错误，错误信息：${error}` })
      }
    },
    async fetchProjectTypeList() {
      try {
        const res = await fetchProjectTypeListAPI()
        if (res.data?.code === 1) {
          this.projectTypeList = res.data.data
          res.data.data.forEach((item) => {
            this.fetchNodeLists(item.id!)
          })
        }
        else { uni.showToast({ title: `${res.data?.msg}`, icon: 'none' }) }
      }
      catch (error) {
        uni.showToast({ title: `请求错误，错误信息：${error}` })
      }
    },
    async fetchCaseTypeList() {
      try {
        const res = await fetchCaseTypeListAPI()
        if (res.data?.code === 1)
          this.caseTypeList = res.data.data
        else
          uni.showToast({ title: `${res.data?.msg}`, icon: 'none' })
      }
      catch (error) {
        uni.showToast({ title: `请求错误，错误信息：${error}` })
      }
    },
    async fetchNodeLists(projectTypeId: string) {
      try {
        const res = await fetchNodeListAPI(projectTypeId)
        if (res.data?.code === 1)
          this.nodeLists[projectTypeId] = res.data.data
        else
          uni.showToast({ title: `${res.data?.msg}`, icon: 'none' })
      }
      catch (error) {
        uni.showToast({ title: `请求错误，错误信息：${error}` })
      }
    },
  },
  persist: { enabled: true },
})
